package com.webapp.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cgame.game.Game;
import com.cgame.game.GameFactory;
import com.cgame.vo.Citizen;
import com.cgame.vo.Forsee;
import com.cgame.vo.Guard;
import com.cgame.vo.Hunter;
import com.cgame.vo.Idiot;
import com.cgame.vo.Player;
import com.cgame.vo.Witch;
import com.cgame.vo.Wolf;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

@Controller
@RequestMapping("/player")
public class PlayerController extends AbstractController{
	protected static final String VIEW = "/";
	@RequestMapping("/gamelist")
	@ResponseBody
	public String gamelist(){
		JsonObject result=new JsonObject();
		result.add("gamelist", GameFactory.getGameList());
		return result.toString();
	}
	@RequestMapping("/join")
	public String join(String gamename,ModelMap modelMap){
		modelMap.put("game", gamename);
		modelMap.put("rolelist", GameFactory.getGame(gamename).rolelist);
		return VIEW+"/game";
	}
	@RequestMapping("/input")
	@ResponseBody
	public String input(@RequestBody String sParams){
		boolean success=true;
		JsonObject params=new Gson().fromJson(sParams, JsonObject.class);
		Game game=GameFactory.getGame(params.get("game").getAsString());
		Player player=new Player();
		player.id=params.get("number").getAsInt();
		player.password=params.get("password").getAsString();
		if(game.hasPlayer(player)){
			success=game.validatePlayer(player);
		}else{
			player.name=params.get("name").getAsString();
			switch (params.get("role").getAsString()) {
			case "forsee":
				player.role=new Forsee();
				break;
			case "witch":
				player.role=new Witch();
				break;
			case "hunter":
				player.role=new Hunter();
				break;
			case "guard":
				player.role=new Guard();
				break;
			case "idiot":
				player.role=new Idiot();
				break;
			case "wolf":
				player.role=new Wolf();
				break;
			case "citizen":
				player.role=new Citizen();
				break;
			default:
				break;
			}
			game.inputPlayer(player);
		}
		//结果
		JsonObject result=new JsonObject();
		if(success){
			result.addProperty("msg", "验证成功");
		}else{
			result.addProperty("msg", "验证失败，请重新输入");
		}
		result.addProperty("success", success);
		result.addProperty("rolename", game.playerMap.get(player.id+"").role.rolename);
		result.addProperty("firstDay", ((game.day-1)==1));
		result.addProperty("god", ((game.day-1)==1));
		result.addProperty("godword", game.godword);
		result.add("playerList", game.getPlayerList());
		return result.toString();
	}
	@RequestMapping("/night")
	@ResponseBody
	public String night(@RequestBody String sParams){
		JsonObject params=new Gson().fromJson(sParams, JsonObject.class);
		Game game = GameFactory.getGame(params.get("game").getAsString());
		JsonObject result=new JsonObject();
		Player me=game.playerMap.get(params.get("number").getAsString());
		if(!(game.day>1||game.gameReady())){
			result.addProperty("msg", "游戏配置不对，角色输入有误或等待其他玩家进入！");
			result.addProperty("success", false);
			return result.toString();
		}
		if(me.isOut){
			result.addProperty("msg", "你已经死了！");
			result.addProperty("success", false);
			return result.toString();
		}
		if(game.startNight(params.get("number").getAsString())){
			result.addProperty("msg", "天黑了");
			result.add("playerList",game.getPlayerList());
			result.addProperty("success", true);
		}else{
			result.addProperty("msg", "有人还没有醒来！");
			result.addProperty("success", false);
		}
		return result.toString();
	}
	@RequestMapping("/kill")
	@ResponseBody
	public String kill(@RequestBody String sParams){
		JsonObject params=new Gson().fromJson(sParams, JsonObject.class);
		Game game = GameFactory.getGame(params.get("game").getAsString());
		boolean haskilled = !game.kill(params.get("person").getAsJsonObject());
		
		//结果
		JsonObject result=new JsonObject();
		if(haskilled){
			result.addProperty("msg", "你的队友已经杀过人了！");
			result.addProperty("done", true);
		}else{
			result.addProperty("msg", "狼刀成功");
			game.night.wolfready=true;
			result.addProperty("done", true);
		}
		return result.toString();
	}
	@RequestMapping("/refresh")
	@ResponseBody
	public String refresh(@RequestBody String gamename){
		Game game = GameFactory.getGame(gamename);
		
		return game.getPlayerList().toString();
	}
	@RequestMapping("/wrefresh")
	@ResponseBody
	public String wrefresh(@RequestBody String gamename){
		Game game = GameFactory.getGame(gamename);
		JsonObject result=new JsonObject();
		if(game.night.hasKilled){
			result.addProperty("wolfdone", true);
			if(((Witch)(game.witch.role)).hasHeal){
				result.add("personDead", game.night.killed);
			}
			result.addProperty("hasHeal",((Witch)(game.witch.role)).hasHeal);
			result.addProperty("hasPoison",((Witch)(game.witch.role)).hasPoison);
		}else {
			result.addProperty("wolfdone", false);
		}
		return result.toString();
	}

	@RequestMapping("/heal")
	@ResponseBody
	public String heal(@RequestBody String gamename){
		Game game = GameFactory.getGame(gamename);
		JsonObject result=new JsonObject();
		if(game.night.killed.get("number").getAsInt()==game.witch.id){
			result.addProperty("msg", "女巫不能自救");
			result.addProperty("done", false);
		}else if(game.night.witchready){
			result.addProperty("msg", "行动结束了！");
		}else{
			game.night.heal();
			result.addProperty("msg", "使用解药成功！");
			result.addProperty("done", true);
			game.night.witchready=true;
		}
		return result.toString();
	}
	@RequestMapping("/witchkill")
	@ResponseBody
	public String witchkill(@RequestBody String sParams){
		JsonObject params=new Gson().fromJson(sParams, JsonObject.class);
		JsonObject result=new JsonObject();
		Game game = GameFactory.getGame(params.get("game").getAsString());
		if(game.night.witchready){
			result.addProperty("msg", "行动结束了！");
			result.addProperty("done", true);
		}else{
			game.night.poisoned=params.get("person").getAsJsonObject();
			result.addProperty("msg", "使用毒药成功！");
			result.addProperty("done", true);
			game.night.witchready=true;
			
		}
		return result.toString();
	}

	@RequestMapping("/witchpass")
	@ResponseBody
	public String witchpass(@RequestBody String gamename){
		Game game = GameFactory.getGame(gamename);
		JsonObject result=new JsonObject();
		result.addProperty("done", true);
		game.night.witchready=true;
		return result.toString();
	}
	@RequestMapping("/guardpass")
	@ResponseBody
	public String guardpass(@RequestBody String gamename){
		Game game = GameFactory.getGame(gamename);
		JsonObject result=new JsonObject();
		result.addProperty("done", true);
		game.night.guardready=true;
		((Guard)game.guard.role).lastGuard=0;
		return result.toString();
	}
	@RequestMapping("/forsee")
	@ResponseBody
	public String forsee(@RequestBody String sParams){
		JsonObject params=new Gson().fromJson(sParams, JsonObject.class);
		JsonObject result=new JsonObject();
		Game game = GameFactory.getGame(params.get("game").getAsString());
		Player person = game.playerMap.get(params.get("person").getAsJsonObject().get("number").getAsString());
		String msg=person.id+"号"+person.name+"是";
		if(person.role.isGood){
			msg+="【好人】";
		}else{
			msg+="【狼人】";
		}
		result.addProperty("msg", msg);
		result.addProperty("done", true);
		game.night.forseeready=true;
		return result.toString();
	}
	@RequestMapping("/guard")
	@ResponseBody
	public String guard(@RequestBody String sParams){
		JsonObject params=new Gson().fromJson(sParams, JsonObject.class);
		JsonObject result=new JsonObject();
		Game game = GameFactory.getGame(params.get("game").getAsString());
		if(((Guard)(game.guard.role)).lastGuard==params.get("person").getAsJsonObject().get("number").getAsInt()){
			result.addProperty("msg", "守卫不能连续守护同一个人！");
			result.addProperty("done", false);
		}else {
			result.addProperty("msg", "守护成功！");
			game.night.guarded=params.get("person").getAsJsonObject();
			result.addProperty("done", true);
			((Guard)(game.guard.role)).lastGuard=params.get("person").getAsJsonObject().get("number").getAsInt();
			game.night.guardready=true;
			
		}
		return result.toString();
	}

	@RequestMapping("/day")
	@ResponseBody
	public String day(@RequestBody String sParams){
		JsonObject params=new Gson().fromJson(sParams, JsonObject.class);
		Game game = GameFactory.getGame(params.get("game").getAsString());
		JsonObject result=new JsonObject();
		boolean day=game.day(params.get("number").getAsString());
		result.addProperty("day", day);
		result.addProperty("firstDay", ((game.day-1)==1));	//是否是第一天
		result.addProperty("godword", game.godword);
		result.add("playerList", game.getPlayerList());
		return result.toString();
	}
	@RequestMapping("/vote")
	@ResponseBody
	public String vote(@RequestBody String sParams){
		JsonObject params=new Gson().fromJson(sParams, JsonObject.class);
		Game game = GameFactory.getGame(params.get("game").getAsString());
		JsonObject result=new JsonObject();
		Player vote=game.playerMap.get(params.get("person").getAsJsonObject().get("number").getAsString());
		if(vote.role instanceof Idiot){
			result.addProperty("msg", "他是白痴");
			vote.role.isGod=false;
		}else {
			vote.isOut=true;
		}
		if(game.isFinished()){
			result.addProperty("msg", game.finish);
		}
		result.add("playerList", game.getPlayerList());
		return result.toString();
	}
	@RequestMapping("/tips")
	@ResponseBody
	public String tips(@RequestBody String gamename){
		Game game = GameFactory.getGame(gamename);
		JsonObject result = game.gettips();
		return result.toString();
	}
}
